package fr.univ.context.data.info;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;

/**
 * Check device's network connectivity
 */

public class Connectivity {
	
	private ConnectivityManager connectManager;
	private NetworkInfo infoMobile;
	private NetworkInfo infoWifi;
	private Context context;
	
	public Connectivity(Context context){
		
		this.context = context;
		connectManager = (ConnectivityManager)  context.getSystemService(Context.CONNECTIVITY_SERVICE);	
		
		infoMobile = connectManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		infoWifi = connectManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
	}
	
	// get Internet status
	public String getType(){
		
		String type = null;
		
		if(((infoWifi != null) && (infoWifi.getState() == NetworkInfo.State.CONNECTED)))
			type = "Wifi connected";
		else if(((infoMobile != null) && (infoMobile.getState() == NetworkInfo.State.CONNECTED)))
			type = "Mobile connected";
		else
			type = "No Network";
		
		return type;
	}
	
	// get name of a wireless local area network
	public String getSSID(){
		
		String ssid = null;
		
		if(((infoWifi != null) && (infoWifi.getState() == NetworkInfo.State.CONNECTED))){
			
			WifiManager mng = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
			ssid = mng.getConnectionInfo().getSSID();				    
		}
		
		return ssid;
	}
}
